package com.singerw.pojo;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @version V1.0
 * @Package com.singerw.pojo
 * @auhter 张欣
 * @date 2020/12/29-5:37 PM
 */
public class MyUserDetails implements UserDetails {

    private String id;
    private String username;
    private String password;
    private Collection<? extends GrantedAuthority> authorities;


    // 写一个能直接使用user创建jwtUser的构造器
    public MyUserDetails(TwUser twUser,List<TwRole> roles) {
        this.id = twUser.getId();
        this.username = twUser.getUsername();
        this.password = twUser.getPassword();
        List<SimpleGrantedAuthority> authorities=new ArrayList<>(roles.size());
        for (TwRole role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getRoleName()));
        }
        this.authorities = authorities;

    }


    public Collection<? extends GrantedAuthority> getAuthorities() {

        return authorities;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return username;
    }

    public boolean isAccountNonExpired() {
        return true;
    }

    public boolean isAccountNonLocked() {
        return true;
    }

    public boolean isCredentialsNonExpired() {
        return true;
    }

    public boolean isEnabled() {
        return true;
    }

    @Override
    public String toString() {
        return "JwtUser{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", authorities=" + authorities +
                '}';
    }

}
